package com.maxima.attendance.actions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.maxima.attendance.daos.TimeLogDAO;
import com.opensymphony.xwork2.ActionSupport;

public class WorkFromHomeAction extends ActionSupport implements SessionAware {
	private static final long serialVersionUID = 1L;
	private TimeLogDAO timeLogDAO = new TimeLogDAO();
	private Map<String, Object> map;
	// for logging
	static Logger logger = Logger.getLogger(ProjectAction.class.getName());

	@Override
	public String execute() throws Exception {
		if (SessionManager.validateAdminSession()) {
			return SUCCESS;
		}
		return ERROR;
	}

	public String workFromHome() {
		String values[] = ServletActionContext.getRequest().getParameterValues("duallistbox_demo1[]");
		List<Integer> lst = new ArrayList<Integer>();
		if (values != null) {
			for (String string : values) {
				lst.add(Integer.parseInt(string));
			}
		}
		Object strDate = ServletActionContext.getRequest().getSession().getAttribute(SessionManager.SELECTED_DATE);
		System.out.println("Object is: " + strDate);
		Date date = null;
		if (strDate != null) {
			try {
				date = new SimpleDateFormat("dd/MM/yyyy").parse((String) strDate);
			}
			catch (ParseException e) {
				e.printStackTrace();
			}
		}
		// put date in following call
		System.out.println("Date is: " + date);
		timeLogDAO.setWorkFromHome(lst, true, date);
		// new InitializeList().initWorkFromHomeEmployees();
		logger.info("Work from home info. updated successfully [" + lst + "]");
		return SUCCESS;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	@Override
	public void setSession(Map<String, Object> m) {
		this.map = m;
	}
}
